/**
 * 
 */
package com.wubo.sec.web.action;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.wubo.common.util.StringUtils;
import com.wubo.sec.service.IUrlResourceService;

import flexjson.JSONSerializer;

/**
 * @author wubo
 * @CreateDate 2010-8-9
 * @version 1.0.01
 */
@Controller
public class UrlAuthMgtAction extends BaseSecAction{
	@Autowired
	private IUrlResourceService urlResourceService;
	
	private String urlCode;
	
	private String authCode;
	
	public IUrlResourceService getUrlResourceService() {
		return urlResourceService;
	}

	public void setUrlResourceService(IUrlResourceService urlResourceService) {
		this.urlResourceService = urlResourceService;
	}

	public String getUrlCode() {
		return urlCode;
	}

	public void setUrlCode(String urlCode) {
		this.urlCode = urlCode;
	}

	public String getAuthCode() {
		return authCode;
	}

	public void setAuthCode(String authCode) {
		this.authCode = authCode;
	}
	
	public String show(){
		try{
			authCode = urlResourceService.getAuthCode(urlCode);
			
			logger.debug("Find url's auth .URLCode:"+urlCode+", AuthCode:"+authCode);
			
			jsonSuccess(new JSONSerializer().serialize(authCode));
		}catch(RuntimeException t){
			jsonError(null);
			throw t;
		}
		return null;
	}
	
	public String relate(){
		logger.debug("relate auths to URL .URLCode:"+urlCode+", AuthCode:"+authCode);

		try{

			if(! StringUtils.hasText(authCode)){
				authCode = null;
			}
			urlResourceService.relate( urlCode, authCode );
			
			jsonSuccess(null);
		}catch(RuntimeException t){
			jsonError(null);
			throw t;
		}
		return null;
	}
}
